package com.netflexitysolutions.amazonws.s3.internal.operations;

import java.util.ArrayList;
import java.util.List;

import com.amazonaws.s3.doc._2006_03_01.ListAllMyBuckets;
import com.amazonaws.s3.doc._2006_03_01.ListAllMyBucketsEntry;
import com.amazonaws.s3.doc._2006_03_01.ListAllMyBucketsList;
import com.amazonaws.s3.doc._2006_03_01.ListAllMyBucketsResponse;
import com.amazonaws.s3.doc._2006_03_01.ListAllMyBucketsResult;
import com.netflexitysolutions.amazonws.s3.S3;
import com.netflexitysolutions.amazonws.s3.S3Bucket;

public class ListAllBucketsOperation extends OperationExecutor<ListAllMyBuckets, List<S3Bucket>>{
	
	/**
	 * @param s3
	 */
	public ListAllBucketsOperation(S3 s3) {
		super(s3);
	}

	/* (non-Javadoc)
	 * @see com.netflexitysolutions.amazonws.s3.internal.operations.OperationExecutor#call(java.lang.Object)
	 */
	@Override
	protected List<S3Bucket> call(ListAllMyBuckets request) {
		List<S3Bucket> buckets = new ArrayList<S3Bucket>();
        injectCredentials(request, "ListAllMyBuckets", getS3().getAccessKey(), getS3().getSecretKey());
		ListAllMyBucketsResponse response = getS3().getService().listAllMyBuckets(request);
		ListAllMyBucketsResult result = response.getListAllMyBucketsResponse();
		if(result != null){
			ListAllMyBucketsList bucketList = result.getBuckets();
			if(bucketList != null){
				List<ListAllMyBucketsEntry> bucketEntries = bucketList.getBucket();
				for (ListAllMyBucketsEntry listAllMyBucketsEntry : bucketEntries) {
					S3Bucket bucket = new S3Bucket();
					bucket.setName(listAllMyBucketsEntry.getName());
					bucket.setCreationDate(listAllMyBucketsEntry.getCreationDate().toGregorianCalendar().getTime());
					buckets.add(bucket);
				}
			}
		}
		return buckets;
	}
}
